package interfaces.hud.pilot.equipment;

import java.util.List;

import fileHandling.language.interfaces.HUDText;
import interfaces.hud.BasicHUD;
import interfaces.hud.spectate.SpectateChangeEvent;
import interfaces.hud.spectate.SpectateListener;
import interfaces.hud.spectate.SpectateWidget;
import interfaces.superWidgets.FlowContent;
import interfaces.superWidgets.MinimizeWindow;

import logic.common.player.Player;
import logic.ships.equipmentListener.EquipmentListener;
import logic.ships.equipmentListener.ItemChangedEvent;
import logic.ships.equipmentListener.WeaponChangedEvent;
import logic.ships.hunter.Hunter;
import logic.weapons.Weapon;
import logic.weapons.energyWeapons.EnergyWeapon;
import logic.weapons.projectileWeapons.ProjectileWeapon;

import org.fenggui.StandardWidget;
import org.fenggui.layout.StaticLayout;

public class EquipmentHUD extends MinimizeWindow implements SpectateWidget {
	
	private ItemContent itemContent;
	private EquipmentListener equipmentListener;
	private Hunter hunter;
	private Player player;
	private int contWidth, itemHeight, weaponButtonHeight;
	private FlowContent weaponButtons;
	
	public EquipmentHUD(Player player, BasicHUD hud) {
		super(HUDText.Weapon_Status, Mode.DOWN, hud);
		this.player = player;
		
		int width = hud.getWidth() / 5;
		int height = hud.getWindowHeight();
		setXY(hud.getWidth() - width, 0);
		setSize(width, height);
		
		content.setLayoutManager(new StaticLayout());
		
		contWidth = content.getWidth();
		itemHeight = content.getHeight() / 6;
		int weaponContentHeight = content.getHeight() - itemHeight;
		weaponButtonHeight = weaponContentHeight / 6;
		
		weaponButtons = new FlowContent(contWidth, weaponContentHeight, 0, 0, weaponButtonHeight, false);
		content.addWidget(weaponButtons);
		
		equipmentListener = new EquipmentListener() {
			@Override
			public void weaponRemoved(WeaponChangedEvent event) {
				removeWeapon(event.getWeapon());
			}
			
			@Override
			public void weaponAdded(WeaponChangedEvent event) {
				addWeapon(event.getWeapon());
			}
			
			@Override
			public void itemAdded(ItemChangedEvent event) {}
			@Override
			public void itemUsed(ItemChangedEvent event) {}
			@Override
			public void itemRemoved(ItemChangedEvent event) {}
		};
		
		if(player.getHunter() != null) respawn();
		
		layout();
	}
	
	public void respawn() {
		Hunter newHunter = player.getHunter();
		
		if(newHunter != null) {
			if(newHunter != hunter) {
				weaponButtons.clearAllWidgets();
				for(List<Weapon> weaponList : newHunter.getAllWeapons().values()) {
					for(Weapon w : weaponList) {
						addWeapon(w);
					}
				}
				
				hud.addListener(newHunter, equipmentListener);
			}
			
			if(itemContent == null) {
				itemContent = new ItemContent(contWidth, itemHeight, 0, weaponButtons.getHeight(), player, hud);
				content.addWidget(itemContent);
			}
		}
		if(hunter != null) hunter.removeEquipmentListener(equipmentListener);
		
		hunter = newHunter;
	}
	
	public void addWeapon(Weapon w) {
		WeaponButton button = null;
		int buttonWidth = weaponButtons.getWidth();
		
		if(w instanceof EnergyWeapon) button = new WeaponButton(w, buttonWidth, weaponButtonHeight, hud);
		else button = new ProjectileWeaponButton((ProjectileWeapon)w, buttonWidth, weaponButtonHeight, hud);
		
		weaponButtons.addFlowWidget(button);
		
		layout();
	}
	
	public void removeWeapon(Weapon w) {
		WeaponButton weaponButton = null;
		
		for(StandardWidget button : weaponButtons.getFlowWidgets()) {
			if(((WeaponButton)button).getWeapon() == w) {
				weaponButton = (WeaponButton)button;
				break;
			}
		}
		weaponButtons.removeFlowWidget(weaponButton);
		
		layout();
	}
	
	@Override
	public SpectateListener getSpectateListener() {
		return new SpectateListener() {
			@Override
			public void spectating(SpectateChangeEvent event) {
				player = event.getWatchedPlayer();
				respawn();
			}
		};
	}
}